/*
 * @Author: song
 * @Description:技能轨道基类
 * @Date: 2024年04月22日 星期一 19:04:04
 * @Modify:
 */

using System;
using System.Collections.Generic;
using SCore.Function.Fixed;
using UnityEngine;

namespace Function.SkillEditor.Runtime
{
    [Serializable]
    public class SkillClip : ScriptableObject
    {
        [SerializeField] public UnityEngine.Object _asset;
        [SerializeField] public int _startFrame;
        [SerializeField] public int _endFrame;
        [SerializeField] public int _lengthFrame;
        [NonSerialized] protected SkillEntityData _entityData;

        internal void Configure(SkillEntityData skillEntityData)
        {
            _entityData = skillEntityData;
        }

        public virtual void Render(RenderState renderState, IClipRenderEnv renderEnvEnv)
        {
        }

        public virtual void OnEnter()
        {
        }

        public virtual void Tick()
        {
        }

        public virtual void OnExit()
        {
        }
    }


    [Serializable]
    public abstract class SkillTrack : ScriptableObject
    {
        [SerializeField] protected List<SkillClip> _clips = new List<SkillClip>();


        public virtual void Tick(fp runFrame)
        {
            for (int offest = _clips.Count - 1; offest >= 0; offest--)
            {
                var clip = _clips[offest];
                //没在范围不执行
                if (runFrame > clip._endFrame || runFrame < clip._startFrame)
                {
                    continue;
                }

                if (clip._startFrame == runFrame)
                {
                    clip.OnEnter();
                    $"{runFrame} {clip.GetType()}.OnEnter".Log();
                }
                else if (runFrame > clip._startFrame && runFrame < clip._endFrame)
                {
                    clip.Tick();
                }
                else if (clip._endFrame == runFrame)
                {
                    clip.OnExit();
                    $"{runFrame} {clip.GetType()}.OnExit".Log();
                }
            }
        }

        public virtual void Start()
        {
        }

        /// <summary>
        /// 获取最大帧数
        /// </summary>
        /// <returns></returns>
        public int GetMaxFrame()
        {
            int maxFrame = 0;
            foreach (var clip in _clips)
            {
                if (maxFrame < clip._endFrame)
                {
                    maxFrame = clip._endFrame;
                }
            }

            return maxFrame;
        }

        /// <summary>
        /// 得到片段
        /// </summary>
        /// <returns></returns>
        public List<SkillClip> GetClips()
        {
            return _clips;
        }

        /// <summary>
        /// 得到片端    通过索引
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public SkillClip GetClipByIndex(int index)
        {
            if (index <= _clips.Count - 1 && index >= 0)
            {
                return _clips[index];
            }

            return null;
        }

        /// <summary>
        /// 添加片段
        /// </summary>
        /// <param name="skillClip"></param>
        public void AddClip(SkillClip skillClip)
        {
            _clips.Add(skillClip);
        }

        /// <summary>
        /// 移除片段
        /// </summary>
        /// <param name="index"></param>
        public void RemoveClip(int index)
        {
            if (index <= _clips.Count - 1 && index >= 0)
            {
                _clips.RemoveAt(index);
            }
        }

        /// <summary>
        /// 移除片段
        /// </summary>
        /// <param name="skillClip"></param>
        public void RemoveClip(SkillClip skillClip)
        {
            _clips.Remove(skillClip);
        }

        /// <summary>
        /// 清理
        /// </summary>
        public void Clear()
        {
            _clips.Clear();
        }

        /// <summary>
        /// 得到迭代器
        /// </summary>
        /// <returns></returns>
        public List<SkillClip>.Enumerator GetEnumerator()
        {
            return GetClips().GetEnumerator();
        }
    }
}